﻿'         This file is Copyright © 2008-2012 Billy O'Neal and Aommaster
'                            All rights reserved.
'
' This file is released under the 2-Clause BSD license, included in LICENSE.TXT
'                           and on the MHWT website
Imports System.ComponentModel
Imports System.Xml.Serialization
Imports System.IO

Friend Class Archive
    Private filterShown As Boolean = False
    Private Sub populate()
        If filterShown Then
            filterBox.Text = ""
        End If
        '
        'Remove the event handlers pointing to us... we don't want our updating to go into infinite loop
        '
        RemoveHandler GlobalData.archive.ListChanged, AddressOf popFrontend
        RemoveHandler GlobalData.archive.AddingNew, AddressOf popFrontend
        filterBox.Text = ""
        popInner()
        '
        'Readd handlers now that we're done
        '
        AddHandler GlobalData.archive.ListChanged, AddressOf popFrontend
        AddHandler GlobalData.archive.AddingNew, AddressOf popFrontend
    End Sub
    Private Sub popInner()
        'knock off event handlers before clearing the listbox
        For Each item As Topic In topicListBox.Items
            RemoveHandler item.Modified, AddressOf populate
            RemoveHandler item.Deleted, AddressOf populate
        Next
        topicListBox.Items.Clear()
        'sort the list
        Dim display As List(Of Topic) = GlobalData.archive.ToList()
        display.Sort()
        Dim tempHolder As New BindingList(Of Topic)
        For Each item As Topic In display
            tempHolder.Add(item)
        Next
        GlobalData.archive = tempHolder
        For Each item As Topic In GlobalData.archive
            topicListBox.Items.Add(item)
            AddHandler item.Modified, AddressOf populate
            AddHandler item.Deleted, AddressOf populate
        Next
    End Sub
    Private Sub popFrontend(ByVal sender As System.Object, ByVal e As System.EventArgs)
        populate()
    End Sub
    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Private Sub OutstandingTopics_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        RemoveHandler GlobalData.archive.ListChanged, AddressOf popFrontend
        RemoveHandler GlobalData.archive.AddingNew, AddressOf popFrontend
        Using fs As New IO.FileStream(System.IO.Path.Combine(Application.StartupPath, "Data.db"), IO.FileMode.Create)
            Dim formatter As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
            Dim temp As New DataFrontend
            formatter.Serialize(fs, temp)
        End Using
    End Sub

    Private Sub OutstandingTopics_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        populate()
    End Sub
    Private Sub btnWebpage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWebpage.Click
        If topicListBox.SelectedItem IsNot Nothing Then
            For Each marked As Topic In topicListBox.SelectedItems
                marked.browse()
            Next
        Else
            MsgBox("There does not appear to be a link hidden in the air!")
        End If
    End Sub

    Private Sub topicListBox_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles topicListBox.DoubleClick
        If topicListBox.SelectedItem IsNot Nothing Then
            Dim newTopic As Topic = CType(topicListBox.SelectedItem, Topic)
            Using childFrm As New FixForm(newTopic)
                childFrm.MdiParent = Me.MdiParent
                childFrm.Show()
            End Using
        End If
    End Sub

    Private Sub filterBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles filterBox.TextChanged
        Static removed As New List(Of Topic)
        If filterBox.Text <> "" Then
            If Not filterShown Then
                filterShown = True
                btnResetFilter2.Visible = True
                topicListBox.Height -= 39
                topicListBox.Location = New System.Drawing.Point(topicListBox.Location.X, topicListBox.Location.Y + 39)
            End If
            For Each item As Topic In removed
                topicListBox.Items.Add(item)
            Next
            removed.Clear()
            popInner()
            For Each curItem As Topic In topicListBox.Items
                If curItem.ToString().IndexOf(filterBox.Text.ToLower(), StringComparison.OrdinalIgnoreCase) = -1 Then
                    removed.Add(curItem)
                End If
            Next
            For Each curItem As Topic In removed
                topicListBox.Items.Remove(curItem)
            Next
        End If
        If filterBox.Text = "" Then
            filterShown = False
            btnResetFilter2.Visible = False
            topicListBox.Height += 39
            topicListBox.Location = New System.Drawing.Point(topicListBox.Location.X, topicListBox.Location.Y - 39)
            For Each item As Topic In removed
                topicListBox.Items.Add(item)
            Next
            removed.Clear()
            popInner()
        End If
    End Sub

    Private Sub btnResetFilter2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResetFilter2.Click
        filterBox.Text = ""
    End Sub

    Private Sub unarchivebutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles unarchivebutton.Click
        If topicListBox.SelectedItem IsNot Nothing Then
            RemoveHandler GlobalData.archive.ListChanged, AddressOf popFrontend
            RemoveHandler GlobalData.archive.AddingNew, AddressOf popFrontend
            For Each item As Topic In GlobalData.archive
                RemoveHandler item.Modified, AddressOf populate
                RemoveHandler item.Deleted, AddressOf populate
            Next
            Dim tempList As New List(Of Topic)
            For Each marked As Topic In topicListBox.SelectedItems
                tempList.Add(marked)
            Next
            For Each marked As Topic In tempList
                GlobalData.archive.Remove(marked)
                GlobalData.openTopics.Add(marked)
                marked.delete()
            Next
            popInner()
            For Each item As Topic In GlobalData.archive
                AddHandler item.Modified, AddressOf populate
                AddHandler item.Deleted, AddressOf populate
            Next
            AddHandler GlobalData.archive.ListChanged, AddressOf popFrontend
            AddHandler GlobalData.archive.AddingNew, AddressOf popFrontend
        Else
            MsgBox("What do you want me to delete again?")
        End If
    End Sub
End Class
